#!/bin/bash
export PATH=/home/postgres/psql14/bin:$PATH
export PGPASSWORD=123456

bakDataDir=/home/postgres/bak1
bakInstancePort=15432
bakInstanceUser=repl
bakInstanceHost=127.0.0.1

slaveDataDir=/home/postgres/slavedata2/pgdata
slaveImdbPageDirectory=/tmp/slave2pagedb
slaveImdbWalDirectory=/tmp/slave2waldb
slaveLogfile=/home/postgres/slave2logfile
slavePort=15433
slaveConninfo='application_name=pushstandby2 user=repl password=123456 host=127.0.0.1 port=15433 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any'

pushDataDir=/home/postgres/pushdata2/pgdata
pushImdbPageDirectory=/tmp/push2pagedb
pushImdbWalDirectory=/tmp/push2waldb
pushLogfile=/home/postgres/push2logfile
pushPort=15434

if [ ! -d "$bakDataDir" ]; then
    echo "$bakDataDir does not exist!"
    exit 1
fi

if [ "`ls -A $bakDataDir`" != "" ]; then
    echo "$bakDataDir is not enpty!"
    exit 1
fi

if [ ! -d "$slaveDataDir" ]; then
    echo "$slaveDataDir does not exist!"
    exit 1
fi

if [ "`ls -A $slaveDataDir`" != "" ]; then
    echo "$slaveDataDir is not enpty!"
    exit 1
fi

if [ ! -d "$pushDataDir" ]; then
    echo "$pushDataDir does not exist!"
    exit 1
fi

if [ "`ls -A $pushDataDir`" != "" ]; then
    echo "$pushDataDir is not enpty!"
    exit 1
fi

pg_basebackup -F p --progress -X none -h $bakInstanceHost -p $bakInstancePort -U $bakInstanceUser -v -D $bakDataDir
if [ $? -ne 0 ]
then
    echo "$(date "+%F %T"): He3DB pg_basebackup failed!"
    exit 1
fi

rsync -av $bakDataDir/* $pushDataDir/

sed -i 's/^push_standby/#push_standby/g' $pushDataDir/postgresql.conf
sed -i 's/^port/#port/g' $pushDataDir/postgresql.conf
sed -i 's/^lmdb_page_directory/#lmdb_page_directory/g' $pushDataDir/postgresql.conf
sed -i 's/^lmdb_wal_directory/#lmdb_wal_directory/g' $pushDataDir/postgresql.conf
sed -i 's/^he3share/#he3share/g' $pushDataDir/postgresql.conf
sed -i 's/^mpush/#mpush/g' $pushDataDir/postgresql.conf

echo -e "he3_point_in_time_recovery = on" >> $pushDataDir/postgresql.auto.conf

rsync -av --exclude base --exclude global $pushDataDir/* $slaveDataDir/
if [ $? -ne 0 ]
then
    echo "$(date "+%F %T"): sync data file failed!"
    exit 1
fi

ln -s $pushDataDir/base $slaveDataDir/base
ln -s $pushDataDir/global $slaveDataDir/global

echo -e "push_standby=off" >> $slaveDataDir/postgresql.conf
echo -e "port=$slavePort" >> $slaveDataDir/postgresql.conf
echo -e "lmdb_page_directory='$slaveImdbPageDirectory'" >> $slaveDataDir/postgresql.conf
echo -e "lmdb_wal_directory='$slaveImdbWalDirectory'" >> $slaveDataDir/postgresql.conf
echo -e "he3share=off" >> $slaveDataDir/postgresql.conf

sed -i 's/^primary_conninfo/#primary_conninfo/g' $pushDataDir/postgresql.auto.conf
sed -i 's/^primary_conninfo/#primary_conninfo/g' $pushDataDir/postgresql.conf
echo -e "primary_conninfo = '$slaveConninfo'" >> $pushDataDir/postgresql.conf
echo -e "push_standby=on" >> $pushDataDir/postgresql.conf
echo -e "port=$pushPort" >> $pushDataDir/postgresql.conf
echo -e "lmdb_page_directory='$pushImdbPageDirectory'" >> $pushDataDir/postgresql.conf
echo -e "lmdb_wal_directory='$pushImdbWalDirectory'" >> $pushDataDir/postgresql.conf

rm -rf $slaveImdbPageDirectory $slaveImdbWalDirectory $pushImdbPageDirectory $pushImdbWalDirectory

chmod 0750 $slaveDataDir -R
chmod 0750 $pushDataDir -R

pg_ctl -D $slaveDataDir -l $slaveLogfile start
if [ $? -ne 0 ]
then
    echo "$(date "+%F %T"): He3DB slave instance start failed!"
    exit 1
fi

pg_ctl -D $pushDataDir -l $pushLogfile start
if [ $? -ne 0 ]
then
    echo "$(date "+%F %T"): He3DB push instance start failed!"
    exit 1
fi

sed -i 's/^he3_point_in_time_recovery/#he3_point_in_time_recovery/g' $slaveDataDir/postgresql.auto.conf
sed -i 's/^he3_point_in_time_recovery/#he3_point_in_time_recovery/g' $pushDataDir/postgresql.auto.conf